package com.lbj.core.app

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.Window
import androidx.databinding.ViewDataBinding
import com.blankj.utilcode.util.BarUtils
import com.lbj.core.R
import com.lbj.core.ext.finishPageAnim
import com.lbj.core.ext.toLog
import com.lbj.core.widget.common.CommonTitle
import com.lbj.mvvm.base.JetpackVMDBActivity

/**
 * 作者：李炳军
 * 创建日期：2021-03-29  14:20
 * 释义：
 */
abstract class CoreActivity<VM : CoreVM, DB : ViewDataBinding> : JetpackVMDBActivity<VM, DB>() {
    abstract override fun layoutId(): Int

    lateinit var mContext: Context
    lateinit var mActivity: Activity

    override fun initStart() {
        "当前运行的Activity:${javaClass.simpleName}".toLog()
        initBar()
        mContext = this
        mActivity = this
        initView()
        bind()
    }

    abstract fun initView()
    abstract fun bind()

    // 透明状态栏
    private fun initBar() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        BarUtils.setStatusBarLightMode(this, true)
    }

    private var mDialog: Dialog? = null

    override fun showLoading(message: String) {
        if (mDialog == null) {
            mDialog = Dialog(mContext, R.style.base_fragment_dialog)
            mDialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
            mDialog?.setContentView(R.layout.core_base_loading)
            mDialog?.setCanceledOnTouchOutside(false)
            val lp = window?.attributes
            lp?.gravity = Gravity.CENTER
            window?.attributes = lp
        }
        mDialog?.show()
    }

    override fun dismissLoading() {
        mDialog?.dismiss()
    }

    override fun onBackPressed() {
        finishPageAnim()
    }
}